package com.customization.huangwm.workflowNodeAction.lims;

import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.HttpRequest;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-06-13
 * @Version 1.0
 * @Description TODO 化學品交收單传给LIMS
 **/
public class NewRawMaterialRequest extends BaseBean implements Action {
    private LogTool log = new LogTool("/log/Lims/huangwm/action/NewRawMaterialRequest", false);
    private HttpRequest http = new HttpRequest();
    private DBTools dbTools = new DBTools();
    private DateTools dateTools = new DateTools();

    @Override
    public String execute(RequestInfo requestInfo) {
        log.writeLog("LIMS 化學品交收單流程推送lims requestid=" + requestInfo.getRequestid());

        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        rs.execute("select * from "+tablename +" where requestid="+requestInfo.getRequestid());
        rs.next();
        String requestNo = rs.getString("lab_samplenum");
        String userCode = dbTools.getWorkCode(rs.getString("creater"));
        String requestPerson = dbTools.getLastname(rs.getString("creater"));
        String sampleIndex = getSampleIndex(rs.getString("chemical_type")).get("index");
        String sampleName = getSampleIndex(rs.getString("chemical_type")).get("name");;

        JSONObject reqData = new JSONObject();
        reqData.put("plantCode", "01");
        reqData.put("action", "newRawMaterialRequest");
        reqData.put("requestTime", requestInfo.getRequestManager().getCurrentDate()+" "+requestInfo.getRequestManager().getCurrentTime());
        reqData.put("requestNo",requestNo);
        reqData.put("userCode",userCode);
        reqData.put("requestPerson",requestPerson);
        reqData.put("sampleIndex",sampleIndex);
        reqData.put("sampleName",sampleName);
        reqData.put("comments","LIMS 化學品交收單流程推送lims requestid=" + requestInfo.getRequestid());

        JSONObject allParams = new JSONObject();//所有的请求参数
        allParams.put("reqNb", dateTools.getTimestamp());
        allParams.put("reqLang", "zh");
        String token = getPropValue("LIMS", "eop_reqToken").isEmpty() ?
                "EapLims20200708D1207F4TEST" : getPropValue("LIMS", "eop_reqToken");
        String url = getPropValue("LIMS", "eop_reqUrl").isEmpty() ?
                "https://testeop.macaowater.com" : getPropValue("LIMS", "eop_reqUrl");
        allParams.put("reqToken", token);
        allParams.put("reqData", reqData);
        log.writeLog("请求参数>>>" + JSONObject.toJSONString(allParams));
        try {
            JSONObject requestResult = http.CallApiPost(url+"/eop/eapToLims/eapapi", JSONObject.toJSONString(allParams));
            log.writeLog("请求结果>>>" + JSONObject.toJSONString(requestResult));

            if (!requestResult.get("state").equals("0") || requestResult==null) {
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent(requestResult.get("msg").toString());
                return FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("http error"+e.getMessage());
            return FAILURE_AND_CONTINUE;
        }

        return SUCCESS;
    }

    private Map<String,String> getSampleIndex(String selectvalue){
        Map<String,String> result = new HashMap<>();
        switch (selectvalue){
            case "0":
                result.put("index","00_4_100");
                result.put("name","聚合氯化鋁");
                break;
            case "1":
                result.put("index","00_4_91");
                result.put("name","NaClO");
                break;
            case "2":
                result.put("index","00_4_116");
                result.put("name","三氯化鐵");
                break;
            case "3":
                result.put("index","00_4_118");
                result.put("name","三氯化鐵");
                break;
            case "4":
                result.put("index","00_4_119");
                result.put("name","氫氧化鈉");
                break;
            case "5":
                result.put("index","00_4_121");
                result.put("name","亞硫酸氫鈉");
                break;
            case "6":
                result.put("index","00_4_120");
                result.put("name","檸檬酸");
                break;
            default:
                result.put("index","0000000");
                result.put("name","未有匹配关系");
                break;
        }
        return result;
    }
}
